package cn.maxhou.crm.controller;

import cn.maxhou.crm.entity.User;
import cn.maxhou.crm.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.catalina.LifecycleState;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

@Controller
public class ExampleController {
    @Resource
    private IUserService userService;

    @GetMapping(value = "/hello/{id}")
    public String getUser(@PathVariable("id") Long usrId, Model model) {
        User user = userService.getUser(usrId);
        model.addAttribute("user", user);
        model.addAttribute("userName", "ktjiaoyu");
        return "demo/hello";
    }
    @GetMapping(value = "/string")
    public String getString(Model model) {
        model.addAttribute("userName", "ktjiaoyu");
        return "demo/string";
    }
    @GetMapping(value = "/inline")
    public String inline(Model model){
        model.addAttribute("userName", "ktjiaoyu");
        return "demo/inline";
    }

    @GetMapping(value = "/string2")
    public String getString2(Model model, HttpServletRequest request) {
        model.addAttribute("userName", "ktjiaoyu");
        request.setAttribute("test", "request");
        request.getSession().setAttribute("test", "session");
        model.addAttribute("date", new Date());
        return "demo/string2";
    }

    @GetMapping(value = "/list")
    public String list(Model model){
        List<User> list =userService.findAllUsers();
        model.addAttribute("users", list);
        return "demo/list";
    }
}
